﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Store
{
    public partial class 入库判定结果 : System.Web.UI.Page
    {
        private string sql = @"select [产品入库单].[ID]
          ,[产品].[品名]+'('+[产品].[钢级]+')'+[产品].[规格] as [产品类别]
          ,[生产单位].[名称] as [生产单位]
          ,[原料供货单位].[名称] as [原料供货单位]
           ,[产品入库单].[序号] as [入库序号]
           ,[产品入库单].炉号 
          ,[产品入库单].[总重量]
          ,[产品入库单].[总长度]
           ,[产品入库单].[总件数]
           ,[产品入库单].[日期]
          ,[产品入库单].[检定日期]
           ,[产品入库单].[是否合格]
           from [产品入库单],[产品],[生产单位],[原料供货单位]
           where [产品].[ID]=[产品入库单].[产品类别ID] and [生产单位].[ID]=[产品入库单].[生产单位ID] and [原料供货单位].[ID]=[产品入库单].[供货单位ID]
                and [产品入库单].[是否合格]=1";
        string isreedonly = "";
        string ishege = "true";
        protected void Page_Load(object sender, EventArgs e)
        {

            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                shengchanxian.Visible = false;
                isreedonly = Request.QueryString["isreadonly"];
                //ishege = Request.QueryString["ishege"];

                //if (isreedonly == "true")
                //{
                //    this.Panel1.Visible = false;
                //}
                //else
                //{
                //    this.Panel1.Visible = true;
                //}
                //判断是否合格
                if (ishege == "true")
                {
                    sql = sql + " and [产品入库单].[产品类别ID]=" + Request.QueryString["id"] + " and [产品入库单].[是否合格]='" + ishege + "'";
                }
                else if (ishege == "false")
                {
                    sql = sql + " and [产品入库单].[产品类别ID]=" + Request.QueryString["id"] + " and [产品入库单].[是否合格]='" + ishege + "'";
                }
                else
                {
                    sql = sql + " and [产品入库单].[产品类别ID]=" + Request.QueryString["id"];
                }

                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChanPin_In.aspx");
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == "1")
            {
                if (this.FenChang.Text != "0")
                {
                    sql += " and [产品入库单].[生产单位ID]=" + this.FenChang.Text.Replace("'", "");
                }
            }
            else
            {
                if (this.FenChang1.Text != "0")
                {
                    sql += " and [产品入库单].[生产单位ID]=" + this.FenChang1.Text.Replace("'", "");
                }
            }
            if (this.GongHuo.Text != "0")
            {
                sql += " and [产品入库单].[供货单位ID]=" + this.GongHuo.Text.Replace("'", "");
            }
            //如果选择了5分厂且有生产线定义
            if (FenChang.SelectedValue=="5" && DropDownList1.SelectedValue != "0")
            {
                sql += "and [产品入库单].[生产线]=" + DropDownList1.SelectedValue;
            }
            if (this.XuHao.Text != "")
            {
                sql += " and [产品入库单].[序号] = " + this.XuHao.Text.Replace("'", "");
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                sql += " and [产品入库单].[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                sql += " and [产品入库单].[日期]<='" + Dt.ToShortDateString() + "'";
            }
            //判断是否合格
            // ishege = this.Status.SelectedItem.Value;

            if (ishege == "true")
            {
                sql = sql + " and [产品入库单].[产品类别ID]=" + Request.QueryString["id"] + " and [产品入库单].[是否合格]='" + ishege + "'";
            }
            else if (ishege == "false")
            {
                sql = sql + " and [产品入库单].[产品类别ID]=" + Request.QueryString["id"] + " and [产品入库单].[是否合格]='" + ishege + "'";
            }
            else
            {
                sql = sql + " and [产品入库单].[产品类别ID]=" + Request.QueryString["id"];
            }
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "Exec ChanPin_In_Bill_Delete " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource1.Delete();
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                Response.Redirect("ChanPin_In.aspx?id=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }
        ////明细
        protected void Button_MX_Click(object sender, EventArgs e)
        {


        }
        //送检保存结果按钮
        protected void SongJianChuLi_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[12].Controls[1];
                string ID = this.GridView1.Rows[i].Cells[1].Text;
                if (cb.Checked == true)
                {
                    //this.SqlDataSource1.UpdateCommand = "Exec RuKuTongZhi_ZhiJian_Update " + "true," + ID;
                    //this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    //this.SqlDataSource1.Update();
                    //this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    //this.Message.Text = "·质检信息保存成功";
                    int check = 0;
                    check = Check("true", ID.ToString());
                    if (check != 2)
                    {
                        this.Message.Text = "·质检信息保存成功";
                    }
                    else
                    {
                        this.Message.Text = "·不能对已出库ID为" + ID + "的产品判定入库单更改合格标记！";
                    }

                }
                else if (cb.Checked == false)
                {
                    //this.SqlDataSource1.UpdateCommand = "Exec RuKuTongZhi_ZhiJian_Update " + "false," + ID;
                    //this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    //this.SqlDataSource1.Update();
                    //this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    //this.Message.Text = "·质检信息保存成功";
                    int check = 0;
                    check = Check("false", ID.ToString());
                    if (check != 2)
                    {
                        this.Message.Text = "·质检信息保存成功";
                    }
                    else
                    {
                        this.Message.Text = "·不能对已出库ID为" + ID + "的产品判定入库单更改合格标记！";
                    }
                }
            }

        }
        protected int Check(string check, string ID)
        {

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                Cmd.CommandText = "RuKuTongZhi_ZhiJian_Update";
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.Add(new SqlParameter("@是否合格", SqlDbType.Bit, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, check));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(ID)));
                Cmd.Parameters.Add(new SqlParameter("@返回值", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                Cmd.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                this.Message.Text = err.Message;
            }
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
            return (int)Cmd.Parameters["@返回值"].Value;
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)(sender);
            GridViewRow gvr = (GridViewRow)cb.NamingContainer;
            Int32 rowIndex = gvr.RowIndex;
            string ID = GridView1.DataKeys[rowIndex].Value.ToString();
            //string ID = this.GridView1.Rows[i].Cells[1].Text;
            if (cb.Checked == true)
            {

                int check = 0;
                check = Check("true", ID.ToString());
                if (check != 2)
                {
                    this.Message.Text = "·质检信息保存成功";
                }
                else
                {
                    this.Message.Text = "·不能对已出库ID为" + ID + "的产品判定入库单更改合格标记！";
                }

            }
            else if (cb.Checked == false)
            {

                int check = 0;
                check = Check("false", ID.ToString());
                if (check != 2)
                {
                    this.Message.Text = "·质检信息保存成功";
                }
                else
                {
                    this.Message.Text = "·不能对已出库ID为" + ID + "的产品判定入库单更改合格标记！";
                }
            }

        }

        //生产线变化
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue != "0")
            {
                sql += "and [产品入库单].[生产线]=" + DropDownList1.SelectedValue;
                SqlDataSource1.SelectCommand = sql;
                ViewState["SelectCommand"] = sql;
            }
            else {
                SqlDataSource1.SelectCommand = sql;
            }
        }
        //分厂变化
        protected void FenChang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FenChang.SelectedValue == "5")
            {
                shengchanxian.Visible = true;
            }
            else {
                shengchanxian.Visible = false;
            }
        }
        //protected void LuHao_Click(object sender, EventArgs e)
        //{
        //    int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
        //    LinkButton lb = (LinkButton)GridView1.Rows[row].Cells[6].Controls[1];
        //    string ID = this.GridView1.Rows[row].Cells[1].Text;//入库单ID
        //    Response.Redirect("产品理化参数.aspx?luhao=" + lb.Text.Trim() + "&&id=" + Request.QueryString["id"] + "&&rkid=" + ID);          
        //}      
    }
}
